//
//  ZWPlayerSSView.m
//  WanEr
//
//  Created by 高文萍 on 16/1/20.
//  Copyright © 2016年 ziwokeji. All rights reserved.
//

#import "ZWPlayerSSView.h"
#import "MusicTimeFormatter.h"
#import "PlayerManager.h"

@interface ZWPlayerSSView()<playerManagerDelegate>
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UIButton *downLoadBut;

@property (weak, nonatomic) IBOutlet UILabel *currentTimer;
// 计时器
@property (nonatomic, strong) NSTimer *playTimer;
@end
@implementation ZWPlayerSSView
- (void)awakeFromNib{
    [PlayerManager playerManager].delegate = self;
}
- (void)prepareMusicInfo:(ZWPlayerModel *)musicInfo{
    self.currentTimer.text = @"00:00";

    self.slider.minimumTrackTintColor = [UIColor colorWithRed:1.000 green:0.600 blue:0.325 alpha:1.000];
}


- (void)playMusicWithFormatString:(NSString *)string{
//    self.slider.value = [MusicTimeFormatter getSecondsFormatByString:string];
    self.currentTimer.text = string;
    
}
// 滑条滑动执行的方法
- (IBAction)didTimeSliderValueChanged:(UISlider *)sender {
    [[PlayerManager playerManager]musicSeekToTime:sender.value];
}
#pragma mark -- delegate
- (void)didPlayChangeStatus:(NSString *)time{
    self.currentTimer.text = time;
}
- (void)didMusicCutWithMusicInfo:(ZWPlayerModel *)musicInfo{
    
    // 控制台
    
    [self prepareMusicInfo:musicInfo];
    
}

#pragma --- 添加计时器 滑条滚动时间---
- (void)addAllTimer
{
    NSTimer *playTimer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(changePlayTimer) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:playTimer forMode:NSDefaultRunLoopMode];
    
    self.playTimer = playTimer;
    
}

#pragma --- 对于音乐的计时器变化,slider应该跟着变.
- (void)changePlayTimer
{
    float value = CMTimeGetSeconds([[PlayerManager playerManager] player].currentItem.currentTime);
    
    self.slider.value = value;
}

#pragma mark -- 将要播放时调用的方法
- (void)prepareMusic:(NSInteger)index{
    [[PlayerManager playerManager] prepareMusic:index];
    [[PlayerManager playerManager] musicPlay];
    self.slider.maximumValue = 20;// 总时间
//    NSLog(@"最大时间SliderMaximumValue:%g",self.slider.maximumValue);
    [self addAllTimer];
}
@end
